﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class AllowanceCalcManageController : AdminBaseController
    {
        //
        // GET: /AllowanceCalcManage/
        [AuthorityActionFilter("m_AllowanceCalcManage", Order = 1)]
        [LogPageActionFilter("补贴计算", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FYear=" + OperateSession.get(GlobalConstant.PARAM_SYSYEAR);
            List<Model.AllowanceIssue> AllowanceIssueList = new List<Model.AllowanceIssue>();
            AllowanceIssueList = GetAllowanceIssueListList(strwhere, 1);
            BLL.AllowanceClass allowanceClassBLL = new BLL.AllowanceClass();
            ViewData["AllowanceClassList"] = allowanceClassBLL.GetModelList("");
            return View(AllowanceIssueList);
        }


        #region  grid绑定
        public ActionResult AllowanceCalcManageListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = " 1=1 ";
            string FYear = Request.Params["FYear"];
            string FAllowanceClassNo = Request.Params["FAllowanceClassNo"];
            string issueID = Request.Params["issueID"];
            if (FYear != "")
            {
                strwhere += " and FYear=" + FYear;
            }
            if (FAllowanceClassNo != "")
            {
                strwhere += " and FAllowanceClassNo='" + FAllowanceClassNo + "'";
            }
            if (issueID != "")
            {
                strwhere += " and FIssueNo='" + issueID + "'";
            }

            List<Model.AllowanceIssue> AllowanceIssueList = new List<Model.AllowanceIssue>();
            AllowanceIssueList = GetAllowanceIssueListList(strwhere, pageIndex);
            return PartialView("AllowanceCalcManageListPartial", AllowanceIssueList);
        }

        public ActionResult SelectCompanyListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string FAllowanceClassNo = Request.Params["FAllowanceClassNo"];
            string strwhere = "FStatus=3 and evalFStatus='2' and FAllowanceClassNo='" + FAllowanceClassNo + "' and FIssueID is null";
            string NSRMC = Request.Params["NSRMC"];
            string NSRBM = Request.Params["NSRBM"];
            string FFinancePropNo = Request.Params["FFinancePropNo"];
            string FEnterprisePropNo = Request.Params["FEnterprisePropNo"];
            if (NSRMC != null && NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
            }
            if (NSRBM != null && NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
            }
           
            if (FEnterprisePropNo != null && FEnterprisePropNo != "" && FEnterprisePropNo != "null")
            {
                strwhere += " and FNewEnterprisePropNo='" + FEnterprisePropNo + "'";

            }
            if (FFinancePropNo != null && FFinancePropNo != "" && FFinancePropNo != "null")
            {
                strwhere += " and FNewFinancePropNo='" + FFinancePropNo + "'";
            }
          
            List<Model.VTaxAuditPolicyEval> VTaxAuditPolicyEvalList = new List<Model.VTaxAuditPolicyEval>();
            VTaxAuditPolicyEvalList = GetVTaxAuditPolicyEvalListList(strwhere, pageIndex);
            return PartialView("SelectCompanyListPartial", VTaxAuditPolicyEvalList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.VTaxAuditPolicyEval> GetVTaxAuditPolicyEvalListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.VTaxAuditPolicyEval VTaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
            string fiedls = "KeyNo";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.v_TaxAuditPolicyEval", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "NSRBM", out total);
            List<Model.VTaxAuditPolicyEval> VTaxAuditPolicyEvalList = VTaxAuditPolicyEvalBLL.DataTableToList(ds.Tables[0]);
            foreach (Model.VTaxAuditPolicyEval vt in VTaxAuditPolicyEvalList)
            {
                vt.FAllowanceAmount = vt.FAllowanceAmount / 10000;
                vt.YBTJE = vt.YBTJE / 10000;
                vt.FBaseAmount = (vt.FBaseAmount == null ? 0 : decimal.Round(decimal.Parse(vt.FBaseAmount.ToString()), 0));
                vt.se = (vt.se == null ? 0 : decimal.Round(decimal.Parse(vt.se.ToString()), 0));
                vt.FOuterRate = (vt.FOuterRate == null ? 0 : decimal.Round(decimal.Parse(vt.FOuterRate.ToString())*100, 2));
                vt.FInnerRate = (vt.FInnerRate == null ? 0 : decimal.Round(decimal.Parse(vt.FInnerRate.ToString()) * 100, 2));
            }
            var q = (from nm in VTaxAuditPolicyEvalList
                     group nm by
                         new
                         {
                             t1 = nm.NSRDZDAH
                         } into g
                     select new
                     {
                         NSRDZDAH = g.Key.t1
                     });
            ViewData["hus"] = q.Count();

            List<Model.VTaxAuditPolicyEval> VTaxAuditPolicyEvalAllList = new List<Model.VTaxAuditPolicyEval>();
            total = VTaxAuditPolicyEvalBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                VTaxAuditPolicyEvalAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (VTaxAuditPolicyEvalList.Count > i)
                {
                    VTaxAuditPolicyEvalAllList[((pageIndex - 1) * pageRowSize) + i] = VTaxAuditPolicyEvalList[i];
                }
            }

 
            return VTaxAuditPolicyEvalAllList;
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.AllowanceIssue> GetAllowanceIssueListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.AllowanceIssue AllowanceIssueBLL = new BLL.AllowanceIssue();
            string fiedls = "FIssueID";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.v_AllowanceCalcManageShow", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FIssueID", out total);
            List<Model.AllowanceIssue> AllowanceIssueList = AllowanceIssueBLL.DataTableToList(ds.Tables[0]);
            List<Model.AllowanceIssue> AllowanceIssueAllList = new List<Model.AllowanceIssue>();
            total = AllowanceIssueBLL.GetRecordCount(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                AllowanceIssueAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (AllowanceIssueList.Count > i)
                {
                    AllowanceIssueAllList[((pageIndex - 1) * pageRowSize) + i] = AllowanceIssueList[i];
                }
            }

            return AllowanceIssueAllList;
        }


        #endregion  grid绑定


        public ActionResult SelectCompany(string FAllowanceClassNo, string issueID)
        {
            ViewData["FAllowanceClassNo"] = FAllowanceClassNo;
            ViewData["issueID"] = issueID;
            string strwhere = " FStatus='3'  and evalFStatus='2' and  FAllowanceClassNo='" + FAllowanceClassNo + "'  and FIssueID is null";
            List<Model.VTaxAuditPolicyEval> VTaxAuditPolicyEvalList = new List<Model.VTaxAuditPolicyEval>();
            VTaxAuditPolicyEvalList = GetVTaxAuditPolicyEvalListList(strwhere, 1);
            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            return View(VTaxAuditPolicyEvalList);
        }

        public ActionResult Save()
        {
            string FIssueID = Fetch.Post("hidFIssueID");
            string type = Fetch.Post("hidSType");
            ViewData["SType"] = type;
            try
            {
                switch (type)
                {
                    case "ALL":
                        {
                            BLL.AllowanceIssue allowanceIssueBLL = new BLL.AllowanceIssue();
                            BLL.VTaxAuditPolicyEval vtaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
                            DataTable dt = vtaxAuditPolicyEvalBLL.GetList("FIssueID='" + FIssueID + "' and FStatus='0'").Tables[0];
                            List<Model.AllowanceIssueEntry> allowanceIssueEntryList = new List<Model.AllowanceIssueEntry>();
                            BLL.AllowanceIssueEntry AllowanceIssueEntryBLL = new BLL.AllowanceIssueEntry();
                            List<Model.AllowanceIssueEntry> list = AllowanceIssueEntryBLL.GetModelList("FIssueID='" + FIssueID + "' and FStatus=0");
                            // List<Model.AllowanceIssueEntry> list = AllowanceIssueEntryBLL.GetModelList("FIssueID='" + id + "' and FStatus=0");
                            if (list.Count == 0)
                            {

                                foreach (DataRow dr in dt.Rows)
                                {
                                    Model.AllowanceIssueEntry model = new Model.AllowanceIssueEntry();
                                    model.FAllowanceBalAmt = decimal.Parse(dr["FAllowanceAmount"].ToString());
                                    model.FAllowancePeriodAmt = decimal.Parse(dr["YBTJE"].ToString());
                                    if (Fetch.Post(dr["FTaxClassAllowanceNo"].ToString() + dr["NSRDZDAH"].ToString() + "BTJE") != "")
                                    {
                                        model.FFactAllowanceAmt = decimal.Parse(Fetch.Post(dr["FTaxClassAllowanceNo"].ToString() + dr["NSRDZDAH"].ToString() + "BTJE")) * 10000;
                                    }
                                    else
                                    {
                                        model.FFactAllowanceAmt = 0;
                                    }
                                    if (model.FFactAllowanceAmt > (model.FAllowanceBalAmt - model.FAllowancePeriodAmt))
                                    {
                                        ViewData["jsAlert"] = "本期补贴金额不可以大于初始金额";
                                        return AllowanceCalcEdit(FIssueID);
                                    }
                                    model.FIssueID = FIssueID;
                                    model.FTaxClassAllowanceNo = dr["FTaxClassAllowanceNo"].ToString();
                                    model.FYear = decimal.Parse(dr["FYear"].ToString());
                                    model.NSRDZDAH = decimal.Parse(dr["NSRDZDAH"].ToString());
                                    model.FStatus = "1";
                                    allowanceIssueEntryList.Add(model);
                                }

                                allowanceIssueBLL.IssueToOK(FIssueID, allowanceIssueEntryList);
                                WorkFlow("t_AllowanceIssue", "10", FIssueID, "start", "");
                                ViewData["jsAlert"] = "操作成功";
                            }
                            else
                            {
                                ViewData["jsAlert"] = "还有" + list.Count + "条数据未处理";
                            }
                            break;
                        }
                    case "Portion":
                        {

                            BLL.VTaxAuditPolicyEval vtaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
                            DataTable dt = vtaxAuditPolicyEvalBLL.GetList("FIssueID='" + FIssueID + "'").Tables[0];
                            DataTable newdt = new DataTable();
                            foreach (DataColumn dc in dt.Columns)
                            {
                                newdt.Columns.Add(dc.ColumnName, dc.DataType);
                            }
                            string checkNoALL = Fetch.Post("hidcheckNoALL");
                            string[] checkNoList = checkNoALL.Split(',');
                            foreach (string no in checkNoList)
                            {
                                string[] noSonList = no.Split('|');

                                DataRow[] drs = dt.Select("FTaxAuditId='" + noSonList[0] + "' and FYear='" + noSonList[1] + "' and FTaxClassAllowanceNo='" + noSonList[2] + "'");

                                foreach (DataRow dr in drs)
                                {
                                    newdt.Rows.Add(dr.ItemArray);
                                }
                            }

                            List<Model.AllowanceIssueEntry> allowanceIssueEntryList = new List<Model.AllowanceIssueEntry>();

                            foreach (DataRow dr in newdt.Rows)
                            {
                                Model.AllowanceIssueEntry model = new Model.AllowanceIssueEntry();
                                model.FAllowanceBalAmt = decimal.Parse(dr["FAllowanceAmount"].ToString());
                                model.FAllowancePeriodAmt = decimal.Parse(dr["YBTJE"].ToString());

                                if (Fetch.Post(dr["FTaxClassAllowanceNo"].ToString() + dr["NSRDZDAH"].ToString() + "BTJE") != "")
                                {
                                    model.FFactAllowanceAmt = decimal.Parse(Fetch.Post(dr["FTaxClassAllowanceNo"].ToString() + dr["NSRDZDAH"].ToString() + "BTJE")) * 10000;
                                }
                                else
                                {
                                    model.FFactAllowanceAmt = 0;
                                }
                                if (model.FFactAllowanceAmt > (model.FAllowanceBalAmt - model.FAllowancePeriodAmt))
                                {
                                    ViewData["jsAlert"] = "本期补贴金额不可以大于初始金额";
                                    return AllowanceCalcEdit(FIssueID);
                                }
                                model.FIssueID = FIssueID;
                                model.FTaxClassAllowanceNo = dr["FTaxClassAllowanceNo"].ToString();
                                model.FYear = decimal.Parse(dr["FYear"].ToString());
                                model.NSRDZDAH = decimal.Parse(dr["NSRDZDAH"].ToString());
                                model.FStatus = "1";
                                allowanceIssueEntryList.Add(model);
                            }

                            BLL.AllowanceIssue allowanceIssueBLL = new BLL.AllowanceIssue();
                            allowanceIssueBLL.IssueToOKPortion(allowanceIssueEntryList);

                            ViewData["jsAlert"] = "操作成功";
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }
            
            return AllowanceCalcEdit(FIssueID);
        }

        public string OpenToIssue(string taxAuditId, string year, string taxClassAllowanceNo, string FIssueID,string sum,string ybtje)
        {
            try
            {
                BLL.TaxAuditPolicyEval taxAuditPolicyEvalBLL = new BLL.TaxAuditPolicyEval();
                BLL.AllowanceIssueEntry allowanceIssueEntryBLL = new BLL.AllowanceIssueEntry();
                Model.TaxAuditPolicyEval model = taxAuditPolicyEvalBLL.GetModel(taxAuditId, year, taxClassAllowanceNo);
                List<Model.AllowanceIssueEntry> Listmodel = allowanceIssueEntryBLL.GetModelList("FIssueID='" + FIssueID + "' and FTaxClassAllowanceNo='" + taxClassAllowanceNo + "' and NSRDZDAH='" + model.NSRDZDAH + "' ");
                string s = "";
                if (model != null)
                {
                    Model.AllowanceIssueEntry model2 = new Model.AllowanceIssueEntry();
                    string type = "";
                    if (Listmodel.Count == 0)
                    {
                        type = "ADD";
                        model2 = new Model.AllowanceIssueEntry();
                        model2.FAllowanceBalAmt = decimal.Parse(model.FAllowanceAmount.ToString());
                        if (ybtje == "")
                        {
                            ybtje = "0";
                        }
                        model2.FAllowancePeriodAmt = decimal.Parse(ybtje) * 10000;
                        if (sum != "")
                        {
                            model2.FFactAllowanceAmt = decimal.Parse(sum) * 10000;
                        }
                        else
                        {
                            model2.FFactAllowanceAmt = 0;
                        }
                        if (model2.FFactAllowanceAmt > (model2.FAllowanceBalAmt - model2.FAllowancePeriodAmt))
                        {
                            return "本期补贴金额不可以大于初始金额";
                        }
                        model2.FIssueID = FIssueID;
                        model2.FTaxClassAllowanceNo = model.FTaxClassAllowanceNo;
                        model2.FYear = model.FYear;
                        model2.NSRDZDAH = decimal.Parse(model.NSRDZDAH);
                        model2.FStatus = "1";
                    }
                    else
                    {
                        type = "Update";
                        model2.FStatus = "1";
                        model2.NSRDZDAH = decimal.Parse(model.NSRDZDAH);
                        model2.FTaxClassAllowanceNo = model.FTaxClassAllowanceNo;
                        model2.FIssueID = model.FIssueID;
                        if (sum != "")
                        {
                            model2.FFactAllowanceAmt = decimal.Parse(sum) * 10000;
                        }
                        else
                        {
                            model2.FFactAllowanceAmt = 0;
                        }

                        if (model2.FFactAllowanceAmt > (Listmodel[0].FAllowanceBalAmt - Listmodel[0].FAllowancePeriodAmt))
                        {
                            return "本期补贴金额不可以大于初始金额";
                        }
                    }
                    taxAuditPolicyEvalBLL.Compute(model, model2, type);
                    BLL.VTaxAuditPolicyEval vtaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
                    DataTable dt = vtaxAuditPolicyEvalBLL.GetAllowanceCalcManageList("FIssueID='" + FIssueID + "' and (FStatus='0' or FStatus is null) and evalFStatus<>'9'").Tables[0];
                    dt = GetEditTable(dt);
                    DataRow[] dr = dt.Select("cz='合计'");
                    if (dr.Count() > 0)
                    {
                        s = "ok||" + dr[0]["se"] + "||" + dr[0]["FAllowanceAmount"] + "||" + dr[0]["YBTJE"] + "||" + dr[0]["BTJE"] + "||" + dr[0]["NSRMC"];
                    }
                    else
                    {
                        return "ex";
                    }
                }
                else
                {
                    return "ex";
                }

                return s;
            }
            catch
            {
                return "ex";
            }
        }    

        public string InvalidToIssue(string taxAuditId, string year, string taxClassAllowanceNo, string FIssueID)
        {
            try
            {
                BLL.TaxAuditPolicyEval taxAuditPolicyEvalBLL = new BLL.TaxAuditPolicyEval();

                Model.TaxAuditPolicyEval model = taxAuditPolicyEvalBLL.GetModel(taxAuditId, year, taxClassAllowanceNo);
                string s = "";
                if (model != null)
                {
                    taxAuditPolicyEvalBLL.Invalid(model);
                    BLL.VTaxAuditPolicyEval vtaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
                    DataTable dt = vtaxAuditPolicyEvalBLL.GetAllowanceCalcManageList("FIssueID='" + FIssueID + "' and (FStatus='0' or FStatus is null) and evalFStatus<>'9'").Tables[0];
                    dt = GetEditTable(dt);
                    DataRow[] dr = dt.Select("cz='合计'");
                    if (dr.Count() > 0)
                    {
                        s = "ok||" + dr[0]["se"] + "||" + dr[0]["FAllowanceAmount"] + "||" + dr[0]["YBTJE"] + "||" + dr[0]["BTJE"] + "||" + dr[0]["NSRMC"];
                    }
                    else
                    {
                        return "ex";
                    }
                }
                else
                {
                    return "ex";
                }

                return s;
            }
            catch
            {
                return "ex";
            }
        }    

        public string DelToIssue(string taxAuditId, string year, string taxClassAllowanceNo, string FIssueID)
        {
            try
            {
                BLL.TaxAuditPolicyEval taxAuditPolicyEvalBLL = new BLL.TaxAuditPolicyEval();
                Model.TaxAuditPolicyEval model = taxAuditPolicyEvalBLL.GetModel(taxAuditId, year, taxClassAllowanceNo);

                taxAuditPolicyEvalBLL.DelToIssue(model);
                BLL.VTaxAuditPolicyEval vtaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
                DataTable dt = vtaxAuditPolicyEvalBLL.GetAllowanceCalcManageList("FIssueID='" + FIssueID + "' and (FStatus='0' or FStatus is null) and evalFStatus<>'9'").Tables[0];
                dt = GetEditTable(dt);
                DataRow[] dr = dt.Select("cz='合计'");

                string s = "";
                if (dr.Count() > 0)
                {
                    s = "ok||" + dr[0]["se"] + "||" + dr[0]["FAllowanceAmount"] + "||" + dr[0]["YBTJE"] + "||" + dr[0]["BTJE"] + "||" + dr[0]["NSRMC"];
                }
                else
                {
                    return "ex";
                }

                return s;
            }
            catch
            {
                return "ex";
            }
        }

        public string AddToIssue(string policyIDList,string issueID)
        {
            try
            {
                List<Model.TaxAuditPolicyEval> taxAuditPolicyEvalList = new List<Model.TaxAuditPolicyEval>();
              
                string[] nolists = policyIDList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string no in nolists)
                {
                    Model.TaxAuditPolicyEval model = new Model.TaxAuditPolicyEval();
                    string[] arr = no.Split('|');
                    model.FTaxAuditId = arr[0];
                    model.FYear = decimal.Parse(arr[1].ToString());
                    model.FTaxClassAllowanceNo = arr[2];
                    model.FIssueID = issueID;
                    taxAuditPolicyEvalList.Add(model);
                }

                BLL.TaxAuditPolicyEval taxAuditPolicyEvalBLL = new BLL.TaxAuditPolicyEval();
                taxAuditPolicyEvalBLL.AddToIssue(taxAuditPolicyEvalList);
                return "ok";
            }
            catch
            {
                return "ex";
            }
        }

        public DataTable GetEditTable(DataTable dt)
        {
            dt.Columns.Add("cz");

            var q = (from nm in dt.AsEnumerable()
                     group nm by
                         new
                         {
                             t1 = nm.Field<decimal>("NSRDZDAH")
                         } into g
                     select new
                     {
                         NSRDZDAH = g.Key.t1
                     });
         

            foreach (DataRow drz in dt.Rows)
            {
                if (drz["FTaxAuditId"].ToString() == "-1")
                {
                    drz["cz"] = "特殊调整";
                    drz["FTaxClassAllowanceName"] = "特殊调整";
                }
                else
                {
                    drz["cz"] = "mx";
                }
                if (drz["BTJE"] != null && drz["BTJE"].ToString() != "")
                {
                    if (drz["FTaxAuditId"].ToString() != "-1")
                    {
                        drz["BTJE"] = decimal.Round(decimal.Parse(drz["BTJE"].ToString()) / 10000, 0);
                    }
                    else
                    {
                        drz["BTJE"] = decimal.Parse(drz["BTJE"].ToString()) / 10000;
                    }
                }
                else
                {
                    drz["BTJE"] = decimal.Round(decimal.Parse(drz["FAllowanceAmount"].ToString()) - decimal.Parse(drz["YBTJE"].ToString()), 0);
                    drz["BTJE"] = decimal.Round(decimal.Parse(drz["BTJE"].ToString()) / 10000, 0);
                }
                drz["FAllowanceAmount"] = decimal.Round(decimal.Parse(drz["FAllowanceAmount"].ToString()) / 10000,0);
                drz["YBTJE"] = decimal.Round(decimal.Parse(drz["YBTJE"].ToString()) / 10000, 0);
            }

            dt.Columns["YBTJE"].DataType = typeof(decimal);
            dt.Columns["FAllowanceAmount"].DataType = typeof(decimal);
            dt.Columns["se"].DataType = typeof(decimal);
            //dt.Columns.Add("BTJE", typeof(decimal), "FAllowanceAmount-YBTJE");
            decimal sumse = dt.AsEnumerable().Sum(p => p.Field<decimal>("FAllowanceAmount"));
            decimal sumse2 = dt.AsEnumerable().Sum(p => p.Field<decimal>("YBTJE"));
            //decimal sumse3 = dt.AsEnumerable().Sum(p => p.Field<decimal>("BTJE"));
            decimal sumse4 = dt.AsEnumerable().Sum(p => p.Field<decimal>("se"));

            DataRow dr = dt.NewRow();
            dr["cz"] = "合计";
            dr["NSRMC"] = "共" + q.Count() + "户";
            dr["FAllowanceAmount"] = sumse;
            dr["YBTJE"] = sumse2;
            //dr["BTJE"] = sumse3;
            dr["se"] = sumse4;

            dt.Rows.Add(dr);


            foreach (DataRow drz in dt.Rows)
            {
                drz["se"] = decimal.Round(decimal.Parse(drz["se"].ToString()), 0);
                if (drz["FBaseAmount"] != null && drz["FBaseAmount"].ToString() != "")
                {
                    drz["FBaseAmount"] = decimal.Round(decimal.Parse(drz["FBaseAmount"].ToString()), 0);
                }
                if (drz["FInnerRate"] != null && drz["FInnerRate"].ToString() != "")
                {
                    drz["FInnerRate"] = decimal.Round(decimal.Parse(drz["FInnerRate"].ToString()) * 100, 2);
                }
                if (drz["FOuterRate"] != null && drz["FOuterRate"].ToString() != "")
                {
                    drz["FOuterRate"] = decimal.Round(decimal.Parse(drz["FOuterRate"].ToString()) * 100, 2);
                }
            }

            DataView dv = dt.DefaultView;

            dv.Sort = "NSRBM desc";

            dt = dv.ToTable();

            return dt;
        }

        public ActionResult AllowanceCalcEdit(string FIssueID)
        {
            BLL.AllowanceIssue allowanceIssueBLL = new BLL.AllowanceIssue();
            Model.AllowanceIssue allowanceIssueModel = new Model.AllowanceIssue();
            if (FIssueID != null && FIssueID != "")
            {
                
                allowanceIssueModel = allowanceIssueBLL.GetModel(FIssueID);
                if (allowanceIssueModel.FStatus == "0")
                {
                    ViewData["type"] = "show";
                }
                else
                {
                    ViewData["type"] = "noshow";
                }
                BLL.VTaxAuditPolicyEval vtaxAuditPolicyEvalBLL = new BLL.VTaxAuditPolicyEval();
                DataTable dt = vtaxAuditPolicyEvalBLL.GetAllowanceCalcManageList("FIssueID='" + FIssueID + "' and (FStatus='0' or FStatus is null or FStatus='-1') and evalFStatus<>'9'").Tables[0];
                ViewData["showdata"] = GetEditTable(dt);
                ViewData["FIssueNo"] = allowanceIssueModel.FIssueNo;
                ViewData["FIssueID"] = allowanceIssueModel.FIssueID;
            }
            else
            {
                ViewData[""] = "请选择期号！";
            }
            return View("AllowanceCalcEdit",allowanceIssueModel);
        }

        public ActionResult LookEntry(string taxClassAllowanceNo, string FTaxAuditId)
        {
            BLL.TaxAuditAdjust taxAuditAdjustBLL = new BLL.TaxAuditAdjust();
            BLL.TaxAuditForm taxAuditFormBLL = new BLL.TaxAuditForm();
            BLL.TaxClassAllowance taxClassAllowanceBLL = new BLL.TaxClassAllowance();
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            DataSet ds = new DataSet();

            Model.TaxAuditForm tAM = taxAuditFormBLL.GetModel(FTaxAuditId);
            ViewData["nsrmc"] = tAM.NSRMC;
            ViewData["taxClassName"] = taxClassAllowanceBLL.GetModel(tAM.FYear, taxClassAllowanceNo).FTaxClassAllowanceName;
            ViewData["rq1"] = tAM.RKRQ_Q.ToString("yyyy-MM-dd");
            ViewData["rq2"] = tAM.RKRQ_Z.ToString("yyyy-MM-dd");
            ds = taxAuditAdjustBLL.GetListForPolicyEntry(FTaxAuditId, taxClassAllowanceNo);


            ds.Tables[0].Columns.Add("xh");
            int i = 1;
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                r["xh"] = i;
                i++;
            }
            BLL.HREmpl hrEmplBLL = new BLL.HREmpl();
            List<Model.TaxAuditLookEntry> taxAuditLookEntryList = new List<Model.TaxAuditLookEntry>();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                Model.TaxAuditLookEntry tAE = new Model.TaxAuditLookEntry();
                tAE.se = decimal.Parse(dr["FAdjustAmount"].ToString());
                tAE.RKRQ = DateTime.Parse(dr["FAdjustDate"].ToString());
                tAE.isAdjust = dr["dataType"].ToString();
                tAE.AdjustNote = dr["FNote"].ToString();
                tAE.xh = int.Parse(dr["xh"].ToString());
                if (dr["FAdjustEmpNo"].ToString() != "")
                {
                    DataSet hrDs = hrEmplBLL.GetEmplFromViewByEmplNo(dr["FAdjustEmpNo"].ToString(), GlobalConstant.ENTITYNO);
                    if (hrDs.Tables[0].Rows.Count > 0)
                    {
                        tAE.AdjustName = hrDs.Tables[0].Rows[0]["FEmpName"].ToString();
                    }
                }
                taxAuditLookEntryList.Add(tAE);
            }

            OperateSession.addList<Model.TaxAuditLookEntry>("AllowanceCalcManageList", taxAuditLookEntryList);
            return View(taxAuditLookEntryList);
        }

        public ActionResult LookEntryPartial()
        {
            return PartialView("LookEntryPartial", OperateSession.getList<Model.TaxAuditLookEntry>("AllowanceCalcManageList").OrderBy(nm => nm.RKRQ));
        }
    }
}
